#include <iostream>
#include <sys/epoll.h>
#include "Error.hpp"
#include "log.hpp"

class Epoller
{
private:
    int _epfd;

public:
    Epoller()
    {
        _epfd = epoll_create(128);
        if(_epfd < 0)
        {
            Log(Fatal, "_epfd create fail");
            exit(EPOLL_CREATE_ERROR);
        }  
    }

    void Commen_FdEvent(int fd, uint32_t arguevent, int type)
    {
        struct epoll_event event;
        event.events = arguevent;
        event.data.fd = fd;
        epoll_ctl(_epfd, type, fd, &event);
    }

    void sup_FdEvent(int fd, uint32_t arguevent)
    {
        Commen_FdEvent(fd, arguevent, EPOLL_CTL_ADD);
    }

    void mod_FdEvent(int fd, uint32_t arguevent)
    {
        Commen_FdEvent(fd, arguevent, EPOLL_CTL_MOD);
    }

    void del_FdEvent(int fd)
    {
        epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, nullptr);
    }

    int wait(int size, struct epoll_event* buffer, int timeout)
    {
        int n = epoll_wait(_epfd, buffer, size, timeout);
        return n;
    }

    ~Epoller()
    {
        if(_epfd > 0) close(_epfd);
    }

};